package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.CmsFood;
import com.example.demo.bean.CmsGuest;
import com.example.demo.bean.CmsRecord;
import com.example.demo.bean.CmsRoom;
import com.example.demo.mapper.CmsFoodMapper;
import com.example.demo.mapper.CmsGuestMapper;
import com.example.demo.mapper.CmsRecordMapper;
import com.example.demo.mapper.CmsRoomMapper;

@Service
public class CmsGuestService {

	@Autowired
	private CmsGuestMapper guestMapper;
	
	@Autowired
	private CmsRoomMapper roomMapper;
	
	@Autowired
	private CmsRecordMapper recordMapper;

	@Autowired
	private CmsFoodMapper foodMapper;
	
	@Autowired
	private CmsRoomService roomService;
	
	@Autowired
	private CmsRecordService recordService;
	
	@Autowired
	CmsFoodService foodService;

	public int insert(CmsGuest guest) {	
		CmsRoom s = roomMapper.selectByPrimaryKey(guest.gethId());
		if(s.getNum()>0) {		
		if(guest.getIdCard()!=null) {
			s.deleteNum();
			roomService.saveOrUpdate(s);
			roomService.setStatus();
			guestMapper.insert(guest);
			int mon = guest.getDay() * s.getPrice();
			recordService.insert(guest.getIdCard(),guest.getName(),
					s.getCategory(),guest.getTel(),mon); 
			return 1;
		}else return -1;
		} 
		return 0;		
	}
	
	public boolean saveOrUpdateCmsGuest(CmsGuest guest) {
		CmsRoom s = roomMapper.selectByPrimaryKey(guest.gethId());		
		
		if(s.getNum()>0) {	
			if(guest.getIdCard()!=null&&!"".contentEquals(guest.getIdCard())) {
				guestMapper.updateByPrimaryKey(guest);
				return true;
			}
	}
	return false;
	}
	
	public int deleteById(String idCard) {
		CmsGuest g = guestMapper.selectByPrimaryKey(idCard);
		CmsRoom r = roomMapper.selectByPrimaryKey(g.gethId());
		r.addNum();
		roomService.saveOrUpdate(r);
		roomService.setStatus();
		return guestMapper.deleteByPrimaryKey(idCard);
	}
	
	public CmsGuest selectById(String id) {
		return guestMapper.selectByPrimaryKey(id);
	}
	
	public List<CmsGuest> findAll(){
		return guestMapper.selectAll();
	}
	
	public int guestPurchase(String idCard,String id,int num1){
//		CmsGuest guest = guestMapper.selectByPrimaryKey(idCard);
		CmsRecord r = recordMapper.selectByPrimaryKey(idCard);
		CmsFood f = foodMapper.selectByPrimaryKey(id);
		System.out.println(num1);
		System.out.println(f.getPrice());
		
		System.out.println(r);		
		int mon = num1 * f.getPrice() + r.getpPay();
		r.setpPay(mon);
		System.out.println("3");
		recordMapper.updateByPrimaryKey(r);
		System.out.println("4");
		int i =foodService.goumai(id,num1);
		return i;
    }
	
	
}



